const tab = require('../models/tab')
const operateObj = require('./utils')

let obj = {
	addData: async (body, res) => {
		await tab.bulkCreate(body)
		res.json({
			code: 0,
			data: null,
			msg: 'success'
		})
	},
	getPage: (body, res) => {
		operateObj.page(tab, body, res, ['name'])
	},
	getList: (body, res) => {
		operateObj.list(tab, body, res)
	},
	getInfo: (body, res) => {
		operateObj.info(tab, body, res)
	},
	updateData: async (body, res) => {
		body.forEach(item => {
			item.updated_time = Date.parse(new Date()) / 1000
		})
		await tab.bulkCreate(body, {
			updateOnDuplicate: ['name', 'updated_time']
		}).then(result => {
			res.json({
				code: 0,
				data: null,
				msg: 'success'
			})
		}).catch(err => {
			res.json({
				code: 500,
				data: null,
				msg: err
			})
		})
	},
	deleteData: (body, res) => {
		operateObj.del(tab, body, res)
	}
}

module.exports = obj
